import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:nyh_motorcade_app/entity/motorcade_entity.dart';
import 'package:nyh_motorcade_app/pages/driver_manage/item/driver_item.dart';
import 'package:nyh_motorcade_app/styles/colors.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:nyh_motorcade_app/widgets/custom_app_bar.dart';
import 'package:nyh_motorcade_app/widgets/custom_popup_menu_item.dart';
import 'package:nyh_motorcade_app/widgets/multi_state/multi_state_widget.dart';
import 'package:nyh_motorcade_app/widgets/text_button_with_icon.dart';
import 'package:pull_to_refresh/pull_to_refresh.dart';
import 'driver_manage_controller.dart';

class DriverManagePage extends StatelessWidget {
  final controller = Get.put(DriverManageController());

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: Colours.color_background,
      appBar: CustomAppBar(
        context: context,
        title: "司机管理",
        actions: [
          IconButton(
            onPressed: () {
              controller.goToSearchDriverPage();
            },
            icon: Image.asset("images/ic_search_black.png"),
          ),
          IconButton(
            onPressed: () {
              controller.goToBatchQuotaPage();
            },
            icon: Image.asset("images/ic_limit.png"),
          ),
          IconButton(
            onPressed: () {
              controller.goToAddDriverPage();
            },
            icon: Image.asset("images/ic_add_account.png"),
          ),
        ],
      ),
      body: GetBuilder<DriverManageController>(
        builder: (controller) {
          return Column(
            children: [
              buildHeaderWidget(),
              buildListWidget(),
            ],
          );
        },
      ),
    );
  }

  Widget buildHeaderWidget() {
    return Container(
      padding: EdgeInsets.only(left: 8.w, right: 16.w),
      height: 40.w,
      child: Row(
        children: [
          Expanded(
            child: Row(
              mainAxisSize: MainAxisSize.min,
              children: [
                TextButtonWithIcon(
                  onPressed: () {
                    showDriverTypeMenu();
                  },
                  icon: Image.asset(controller.showDriverTypeMenu ? "images/ic_arrow_up.png" : "images/ic_arrow_down.png"),
                  label: Text(
                    controller.driverTypeName.isNotEmpty ? controller.driverTypeName : "司机归属",
                    style: TextStyle(
                      color: Colours.color_text_theme,
                      fontSize: 14.sp,
                    ),
                  ),
                  textIconPadding: 5.w,
                  iconTextAlignment: IconTextAlignment.iconRightTextLeft,
                ),
                SizedBox(
                  width: 15.w,
                ),
                Flexible(
                  child: TextButtonWithIcon(
                    onPressed: () {
                      showMotorcadeMenu();
                    },
                    icon: Image.asset(controller.showMotorcadeMenu ? "images/ic_arrow_up.png" : "images/ic_arrow_down.png"),
                    label: Text(
                      controller.motorcadeName.isNotEmpty ? controller.motorcadeName : "全部车队",
                      style: TextStyle(
                        color: Colours.color_text_theme,
                        fontSize: 14.sp,
                      ),
                      maxLines: 1,
                      overflow: TextOverflow.ellipsis,
                    ),
                    textIconPadding: 5.w,
                    iconTextAlignment: IconTextAlignment.iconRightTextLeft,
                  ),
                ),
              ],
            ),
          ),
          SizedBox(
            width: 5.w,
          ),
          Text(
            "共${controller.orderCount}条",
            style: TextStyle(
              color: Colours.color_b7b7c0,
              fontSize: 14.sp,
            ),
          )
        ],
      ),
    );
  }

  Widget buildListWidget() {
    return Expanded(
      child: MultiStateWidget(
        child: SmartRefresher(
          controller: controller.refreshController,
          enablePullDown: true,
          enablePullUp: true,
          onRefresh: controller.onRefresh,
          onLoading: controller.onLoading,
          child: ListView.builder(
            shrinkWrap: true,
            physics: BouncingScrollPhysics(),
            itemCount: controller.driverInfoList.length,
            controller: controller.scrollController,
            itemBuilder: (context, index) {
              return DriverItem(controller.driverInfoList[index]);
            },
          ),
        ),
        state: controller.multiState,
        onRetry: () {
          controller.onRetry();
        },
      ),
    );
  }

  void showDriverTypeMenu() {
    if (controller.driverTypeList.isEmpty) {
      return;
    }
    controller.showDriverTypeMenu = true;
    controller.update();
    showMenu(
      context: Get.context!,
      position: RelativeRect.fromLTRB(16.w, ScreenUtil().statusBarHeight + 56 + 40.w, ScreenUtil().screenWidth, 0.w),
      items: controller.driverTypeList.asMap().keys.map((e) => CustomPopupMenuItem(e, controller.driverTypeList[e].name)).toList(),
      elevation: 5.w,
      shape: RoundedRectangleBorder(
        borderRadius: BorderRadius.circular(10.w),
      ),
    ).then((value) {
      controller.setSelectDriverType(value);
    });
  }

  Future<void> showMotorcadeMenu() async {
    List<MotorcadeEntity> list = await controller.getMotorcadeList();
    if (list.isEmpty) {
      return;
    }
    controller.showMotorcadeMenu = true;
    controller.update();
    showMenu(
      context: Get.context!,
      position: RelativeRect.fromLTRB(100.w, ScreenUtil().statusBarHeight + 56 + 40.w, ScreenUtil().screenWidth, 0.w),
      items: controller.motorcadeList.asMap().keys.map((e) => CustomPopupMenuItem(e, controller.motorcadeList[e].name ?? "")).toList(),
      elevation: 5.w,
      shape: RoundedRectangleBorder(
        borderRadius: BorderRadius.circular(10.w),
      ),
    ).then((value) {
      controller.setSelectMotorcade(value);
    });
  }
}
